package com.zkw.controller;

import com.zkw.pojo.Books;
import com.zkw.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    @Qualifier("bookServiceImpl")
    private BookService bookService;

    //查看所有的书籍
    @RequestMapping("/all")
    public String allBook(Model model){
        List<Books> booksList = bookService.queryAllBook();
        model.addAttribute("list",booksList);
        return "allBook";
    }

    //跳转到添加页面
    @RequestMapping("/toAddPaper")
    public String toAddPaper(){
        return "addBook";
    }

    //添加书籍
    @RequestMapping("/add")
    public String addBook(Books books){
        System.out.println("addBook===>"+books);
        bookService.addBook(books);
        return "redirect:/book/all";
    }

    //跳转到修改页面
    @RequestMapping("/toUpdatePaper/{id}")
    public String toUpdatePaper(@PathVariable int id,Model model){
        Books books = bookService.queryBookById(id);
        model.addAttribute("Qbooks",books);
        return "updateBook";
    }

    //修改书籍
    @RequestMapping("/update")
    public String updateBook(Books books){
        System.out.println("updateBook==>"+books);
        bookService.updateBookById(books);
        return "redirect:/book/all";
    }

    //删除书籍
    @RequestMapping("delete/{id}")
    public String deleteBook(@PathVariable int id){
        bookService.delBookById(id);
        return "redirect:/book/all";
    }

    //查询书籍
    @RequestMapping("/queryName")
    public String queryName(String name,Model model){
        List<Books> booksList = bookService.queryNameBook(name);
        System.err.println(name);
        if (name.equals("")){
            model.addAttribute("error","没有此书籍");
            booksList = bookService.queryAllBook();
        }

        model.addAttribute("list",booksList);
        return "allBook";
    }
}
